<template>
  <div>
    <van-nav-bar
  title="黑马头条-登录"
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="formLogin.mobile"
    name="手机号"
    label="手机号"
    placeholder="请输入手机号"
    required
    :rules="[{ required: true, message: '请填写正确的手机号',pattern:/^1[3-9]\d{9}$/ }]"
  />
  <van-field
    v-model="formLogin.code"
    type="password"
    name="密码"
    label="密码"
    placeholder="请输入密码"
    required
    :rules="[{ required: true, message: '请填写正确的密码' ,pattern: /^\d{6}$/}]"
  />
  <div style="margin: 16px;">
    <van-button round block
    type="info"
    native-type="submit"
    :loading="isdisabled"
    loading-text="努力加载中"
    :disabled="isdisabled"
    to=""
    >提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { LoginApi } from '@/api'
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      isdisabled: false,
      formLogin: {
        mobile: '13888888888',
        code: '246810'
      }
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit () {
      this.isdisabled = true
      // 路由跳转
      // 1，push(可以回退,添加),2,replace(不能回退，替换)
      this.$router.replace({
        path: '/layout'
      })
      try {
        await LoginApi(this.formLogin)
        // const { token, refresh_token } = res.data.data
        // console.log(token, refresh_token)
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.isdisabled = false
      } catch (err) {
        console.log(err)
        Notify({ type: 'warning', message: '手机号或密码错误' })
        this.isdisabled = false
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
